# -*- coding:utf8 -*-
#encoding = utf-8
__author__ = 'man' 

#		_________________________________________________________________________________
#
#						漫鸽网络技术服务(mangenotwork)拥有所有权
#						@www.hbsj.xyz   
#						@www.mangenotwork.com
#			严重声明：原始代码属于"漫鸽网络技术服务(mangenotwork)"的知识产权，拥有法律的保护，
#		请勿抄袭、转载、删除、私用、篡改、破坏...
#
#       
#		简介 ：  shequ.py PC端的接口
#		作者 ： 李漫(man)
#		创建时间:  20190709
#		
#		_________________________________________________________________________________

from flask import Flask,request,g,render_template,redirect,url_for,abort,session,jsonify,current_app
from flask import Blueprint , render_template
from flask import make_response,Response 
import sys,os
import json
import time
import datetime
import redis
from cryptography.fernet import Fernet
#md5
import hashlib
import db.mandb
'''
man_db = db.mandb.DB()
print(man_db.db_connect())
'''
shequ = Blueprint('shequ',__name__)
#函数不能命名 test



#公用方法
import api.public as pub

#图片处理方法
import api.compress_img as compress




'''
					#类型： DB交互
					#执行查询  查询结果
'''
def ManDB(func=None, param=None):
	def deco(func):
		def wrapper(*args,**kwargs):
			man_db = db.mandb.DB()
			return func(man_db,*args,**kwargs)			
		wrapper.__name__ = func.__name__
		return wrapper
	return deco if not func else deco(func)


'''
					#类型： 接口
					#函数不能命名 test
					#使用者 : wap
'''
@shequ.route('/shequ_test', methods=['POST','GET'])
def shequ_test():
	return "This is a Blueprint : shequ Test."





#获取社区最新社区信息
@ManDB
def get_sqhome(man_db):
	sql = "select sq_main.sqmian_id, sq_main.user_uuid, userinfo.user_name, userinfo.user_head, sq_main.sqtitle, \
	sq_main.sqtype, sq_main.sq_sendtime, sq_main.content, sq_main.content_imgs\
	from sq_main LEFT JOIN userinfo \
	on sq_main.user_uuid = userinfo.user_uuid\
	order by sq_main.sq_sendtime+0 DESC\
	LIMIT 0,20\
	;"
	return_datas = man_db.execute_seles(sql)
	return return_datas
"""
# sql :
select sq_main.sqmian_id, sq_main.user_uuid, userinfo.user_name, sq_main.sqtitle, 
sq_main.sqtype, sq_main.sq_sendtime,sq_main.content, sq_main.content_imgs
from sq_main LEFT JOIN userinfo 
on sq_main.user_uuid = userinfo.user_uuid
order by sq_main.sq_sendtime+0 DESC
LIMIT 0,20
;
"""



'''
					#函数,方法
					#社区内容返回文本处理
'''
def format_sqhome(datasinfo):
    add_list = []
    for datas in datasinfo:
        imgs = datas[8]
        imgs = imgs.split('#')
        imglist = []
        for img in imgs:
        	if img != "":
        		imglist.append(img)
        print(imglist)


        '''
        	#评论数
			SELECT count(temp_id)
			from sq_comment 
			where sqmian_id = sqmian_id;
        '''
       	

        returdata = {
        	"type" : datas[5],
        	"infoid" : datas[0],
        	"userid" : datas[1],
        	"user" : datas[2],
        	"userhead" : datas[3],
        	"title" : datas[4],
        	"sendtime" : pub.time_to_date(datas[6]),
        	"imglist" : imglist,
        	"content" : datas[7],
        	}

        add_list.append(returdata)
        returdata = {}
    
    return add_list,datas[5]




#社区
@shequ.route('/sq', methods=['POST','GET'])
def sq_pg():
    getsqinfos = get_sqhome()
    print(getsqinfos)
    returninfo,types = format_sqhome(getsqinfos)

    return render_template( 'shequpg.html', sq=1, sqlist = returninfo)



#系统热搜词 
@ManDB
def get_rsc( man_db):
    sql = "\
        select count_key,count(count_key) from count_query group by count_key ORDER BY count(count_key) DESC\
        LIMIT 0,30;\
    "
    return_datas = man_db.execute_seles(sql)
    return return_datas


#热搜榜
@shequ.route('/rsb', methods=['POST','GET'])
def rsb_pg():
    retuenlist = get_rsc()
    print(retuenlist)
    returninfo = []
    n=1
    for datas in retuenlist:
        listdatas = ["第"+str(n),datas[0],datas[1]]
        returninfo.append(listdatas)
        listdatas = []
        n+=1

    return render_template( 'reshoubang.html', home=1,returnlist = returninfo)


